package Model;

/**
 * @author InnoIrving V. Estrera
 * */
public class User
{
  /**
   * Username used by this registered user.
   * */
  private String my_user_name;

  /**
   * Email used by this registered user.
   * */
  private String my_email;

  /**
   * The user's real name.
   * */
  private String my_real_name;

  /**
   * The user's specific user id
   * */
  private int my_user_id;

  // to prevent instantiation of User with no arguments
  private User()
  {

  }

  /**
   * A two argument constructor that only takes username and an email as
   * parameters. Sets real name to null.
   * 
   * @param the_user_name
   *          The username of this user
   * @param the_email
   *          The email of this user
   * */
  public User(final String the_user_name, final String the_email)
  {
    this(null, the_user_name, the_email);
  }

  /**
   * A 3 argument constructor that takes a real name, username and an email as
   * parameters.
   * 
   * @param the_real_name
   *          The real name of this user
   * @param the_user_name
   *          The username of this user
   * @param the_email
   *          The email of this user
   * */
  public User(final String the_real_name, final String the_user_name,
      final String the_email)
  {
    my_real_name = the_real_name;
    my_user_name = the_user_name;
    my_email = the_email;
  }

  // get methods

  /**
   * Returns the email address of this user in String.
   * 
   * @return The email address as a String Object
   * */
  public String email()
  {
    return my_email;
  }

  /**
   * Returns the user's username in String.
   * 
   * @return The username as a String Object
   * */
  public String userName()
  {
    return my_user_name;
  }

  /**
   * Returns the realname of this user in String. If real name is NULL, returns
   * "NOT GIVEN" as a String instead.
   * 
   * @return The real name of this user as a String Object
   * */
  public String realName()
  {
    return my_real_name;
  }

  /**
   * Returns this user's specific user id in int.
   * 
   * @return The user's id in int.
   * */
  public int id()
  {
    return my_user_id;
  }

  /**
   * Serves as a 'clone' for this User class. Copies every contents and returns
   * a new user to avoid shallow copy.
   * 
   * @return A copy of this User.
   * */
  protected User copy()
  {
    return new User(my_real_name, my_user_name, my_email);
  }

  // set methods

  /**
   * This method accepts a String object and sets this user's real name with the
   * specified parameter.
   * 
   * @param the_real_name
   *          The real name of the user.
   * */
  public void setRealName(final String the_real_name)
  {
    my_real_name = the_real_name;
  }
  
  public boolean isUserName(final String the_user)
  {
    
    if (my_user_name.equals(the_user))
      return true;
    else
      return false;
  }

  /**
   * Returns a String representation of this object with a given username and an
   * email.
   * 
   * @return A String representation of this user
   * */
  public String toString()
  {
    final StringBuilder sb = new StringBuilder();
    sb.append(my_user_name);
    // sb.append(", ");
    // sb.append(my_email);
    return sb.toString();
  }

  /**
   * {@inheritDoc}
   * */
  public int hashCode()
  {
    return toString().hashCode();
  }

  /**
   * Tests equality between users based on username and email.
   * 
   * @param the_other
   *          The other user to be compared.
   * @return Returns true if both users are equal, false otherwise.
   * */
  public boolean equals(final Object the_other)
  {
    if (this == the_other)
    {
      return true;
    } else if ((the_other != null) && (the_other.getClass() == getClass()))
    {
      final User the_other_user = (User) the_other;
      final boolean same_username = my_user_name.equals(the_other_user.userName());
      final boolean same_email = my_email.equals(the_other_user.email());
      return (same_email || same_username);
    } else
    {
      return false;
    }
  }
}
